<template>
  <div class="login">
    <div class="background-image"></div>
    <div class="login-container">
      <h1>Silkpinner后台管理系统</h1>
      <el-form :model="loginForm" class="login-form" ref="loginForm" @submit.native.prevent="login">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="loginForm.username" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="loginForm.password" type="password" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submit">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import axios from "axios";
export default {
  name: "HomeLogin",
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      }
    };
  },
  methods: {
    submit() {
      axios.post('/api/admin', this.loginForm)
          .then(res=>{
            console.log(res.data)
            if (res.data.code === -1){
              alert('账号或密码错误，登录失败！')
            } else {
              const token = res.data.token;
              localStorage.setItem('token', token);
              // alert('登录成功！')
              this.$router.push({
                name:'home'
              })
            }
          })
          .catch(err =>{
            console.log(err)
          })
    }
  }
}
</script>

<style scoped>
.login {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

.background-image {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: -1;
  background-image: url(~@/assets/bg.jpg);
  background-size: cover;
  background-position: center;
  opacity: 1;
}

.login-container {
  position: relative;
  width: 500px;
  margin: 100px auto;
  padding: 20px;
  text-align: center;
  background-color: #f0f2f5;
  z-index: 1;
  opacity: 0.7;
}

.login-form {
  margin-top: 20px;
}
</style>